var appPath = "/bidevapp";

function loadFieldText(oGetterUrl, oCtrlName, oKeyValue, oGetterCtrl)
{
	if (undefined == oGetterCtrl)
		oGetterCtrl = document.all.oDataGetter;
	
	oGetterCtrl.src = oGetterUrl + "?ctrl=" + oCtrlName + "&key=" + oKeyValue + '&ts=' + new Date().getTime();
}
function openSubWindow(oWndUrl, w, h, ta)
{
	var nWidth = (undefined == w ? 222 : w);
	var nHeight = (undefined == h ? 420 : h);
	
	if (undefined == ta)
		ta = "_blank";
	
	open(oWndUrl, ta, 'width=' + nWidth + 'px,height=' + nHeight + 'px,location=no,menubar=no,resizable=yes,scrollbars=auto,status=no,toolbar=no');
}
function openNavigator(oGetterUrl, oCtrlName, w, h, ta, params)
{
	var nWidth = (undefined == w ? 222 : w);
	var nHeight = (undefined == h ? 420 : h);
	
	if (undefined == ta)
		ta = "_blank";
	
	if (undefined == params)
		params = '';
	else
		params = '&' + params;
	
	var openStr = 'width=' + nWidth + ',height=' + nHeight + ',location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no';
	//alert(openStr);
	
	open(oGetterUrl + '?ctrl=' + oCtrlName + params + '&ts=' + new Date().getTime(), ta, openStr);
}
function fieldLostAlert(field, msg)
{
	alert(msg);
	field.focus();
	if (undefined != field.select)
		field.select();
	return false;
}
function appendHiddenFields(oForm, oFieldName, vVal)
{
	var i;
	for (i = 0; i < vVal.length; ++i)
	{
		var oField = document.createElement("<input type='hidden' name='" + oFieldName + "'>");
		oField.value = vVal[i];
		oForm.appendChild(oField);
	}
}
function clearTable(tb)
{
	var c;
	c = tb.rows.length - 1;
	for (; 0 <= c; --c)
		tb.deleteRow(c);
}
// tb: property page's table bar(normally div)
// h: page body height
// w(optional): page body width, default equals to tablebar's width
function initPropertyPageTable(tb, w, h)
{
	clearTable(tb);
	
	tb.className = "pptabbar";
	tb.cellSpacing = 0;
	tb.ppHeight = h;
	tb.style.width = w;
	
	tb.ppArray = new Array();
	var row = tb.insertRow();
	//tb.appendChild(row);
	var lpad = row.insertCell();
	lpad.className = "lpad";
	lpad.appendChild(document.createElement("<img src=\"" + appPath + "/images/spacer.gif\">"));
	
	var rpad = row.insertCell();
	rpad.className = "rpad";
	rpad.appendChild(document.createElement("<img src=\"" + appPath + "/images/spacer.gif\">"));
}
function addPropertyPage(tb, pageDiv, pageTitle, onChangeEval)
{
	pageDiv.style.width = tb.style.width;
	pageDiv.style.height = tb.ppHeight;
	pageDiv.className = "ppdiv";
	
	var row = tb.rows(0);
	
	var curIdx = tb.ppArray.length;
	var tab = row.insertCell(curIdx + 1);
	tab.className = "tabf";
	tab.noWrap = true;
	var tabLink = document.createElement("<a>");
	tabLink.innerText = pageTitle;
	if (undefined != onChangeEval)
		tabLink.href = "javascript: if (" + onChangeEval + ") setPropertyPageActive(" + tb.id + ", " + curIdx + ");";
	else
		tabLink.href = "javascript: setPropertyPageActive(" + tb.id + ", " + curIdx + ");";
	
	tab.appendChild(tabLink);
	
	tb.ppArray[curIdx] = pageDiv;
}
function setPropertyPageActive(tb, pageIndex)
{
	var i;
	var row = tb.rows(0);
	for (i = 0; i < tb.ppArray.length; ++i)
	{
		if (pageIndex < i)
		{
			tb.ppArray[i].style.display = "none";
			tb.ppArray[i].style.position = "absolute";
			tb.ppArray[i].style.top = tb.style.top;
			row.cells(i + 1).className = "tabr";
		}
		else if (pageIndex > i)
		{
			tb.ppArray[i].style.display = "none";
			tb.ppArray[i].style.position = "absolute";
			tb.ppArray[i].style.top = tb.style.top;
			row.cells(i + 1).className = "tabl";
		}
		else
		{
			tb.ppArray[i].style.display = "";
			tb.ppArray[i].style.position = "";
			row.cells(i + 1).className = "tabf";
		}
	}
}
function initSelectControl(ctrl, w, h, colWidths, onItemSelect)
{
	ctrl.className = "scdiv";
	ctrl.style.overflow = "auto";
	ctrl.style.width = w;
	ctrl.style.height = h;
	ctrl.style.clip = 'rect(0 0 ' + h + ' ' + w + ')';
	ctrl.colWidths = colWidths;
	ctrl.onItemSelect = onItemSelect;
	var tab = document.createElement("<table>");
	ctrl.selectTab = tab;
	tab.cellSpacing = 0;
	ctrl.appendChild(tab);
}
function getParentElementByName(c, pTagName)
{
	if (pTagName == c.parentElement.tagName.toLowerCase())
	{
		return c.parentElement;
	}
	
	return getParentElementByName(c.parentElement, pTagName);
}
function selectCtrlOnClickHandler()
{
	if ("td" != event.srcElement.tagName.toLowerCase())
		return true;
	
	var row = getParentElementByName(event.srcElement, "tr");
	var tab = getParentElementByName(row, "table"); //row.parentElement.parentElement;
	var ctrl = getParentElementByName(tab, "div");
	if (undefined != ctrl.onItemSelect)
	{
		if (!ctrl.onItemSelect(row))
			return false;
	}

	var vRow = tab.rows;
	
	for (i = 0; i < vRow.length; ++i)
	{
		renderSelectRow(vRow[i], vRow[i] == row);
	}
	
	return true;
}
function insertCheckboxRow(ctrl, nam, val, isChkd, v)
{
	var tab = ctrl.selectTab;
	var row = tab.insertRow();
	row.onclick = selectCtrlOnClickHandler;
	
	var c = row.insertCell();
	c.width = 16;
	var chk = document.createElement("<input type=\"checkbox\">");
	c.appendChild(chk);
	chk.name = nam;
	chk.value = val;
	chk.checked = isChkd;
	
	if (undefined != v)
	{
		var i;
		for (i = 0; i < v.length; ++i)
		{
			var c = row.insertCell();
			c.innerText = v[i];
			c.noWrap = true;
			if (undefined != ctrl.colWidths)
				c.width = ctrl.colWidths[i];
		}
	}
	
	renderSelectRow(row, 1 == tab.rows.length);
	
	//alert(tab.innerHTML);
	return row;
}
function insertRow(ctrl, v)
{
	var tab = ctrl.selectTab;
	var row = tab.insertRow();
	row.onclick = selectCtrlOnClickHandler;
	
	if (undefined != v)
	{
		var i;
		for (i = 0; i < v.length; ++i)
		{
			var c = row.insertCell();
			c.innerText = v[i];
			c.noWrap = true;
			if (undefined != ctrl.colWidths)
				c.width = ctrl.colWidths[i];
		}
	}
	
	renderSelectRow(row, 1 == tab.rows.length);
	
	//alert(tab.innerHTML);
	return row;
}
function clearAllRows(ctrl)
{
	var tab = ctrl.selectTab;
	while(tab.rows.length)
		tab.deleteRow();
}
function getAllRows(ctrl)
{
	return ctrl.selectTab.rows;
}
function selectRow(ctrl, rowIndex)
{
	var tab = ctrl.selectTab;
	var vRow = tab.rows;
	var i;
	for (i = 0; i < vRow.length; ++i)
	{
		renderSelectRow(vRow[i], rowIndex == i);
	}
	
	if (0 <= rowIndex && rowIndex < vRow.length)
	{
		if (undefined != ctrl.onItemSelect)
			ctrl.onItemSelect(vRow[rowIndex]);
	}
}
function renderSelectRow(row, flag)
{
	if (flag)
		row.className = "selected";
	else
		row.className = "unselected";
}
function deleteSelectedRow(ctrl)
{
	var tab = ctrl.selectTab;
	var i;
	for (i = 0; i < tab.rows.length; ++i)
	{
		if (tab.rows[i].className == "selected")
		{
			tab.deleteRow(i);
			if (i < tab.rows.length)
				selectRow(ctrl, i);
			else if (0 < tab.rows.length)
				selectRow(ctrl, tab.rows.length - 1);
			
			return true;
		}
	}
	
	return false;
}
function getSelectedRow(ctrl)
{
	var tab = ctrl.selectTab;
	var vRow = tab.rows;
	
	for (i = 0; i < vRow.length; ++i)
	{
		if (vRow[i].className == "selected")
			return vRow[i];
	}
	
	return null;
}
function indexOfRow(ctrl, indexOfCell, val)
{
	var tab = ctrl.selectTab;
	var vRow = tab.rows;
	
	for (i = 0; i < vRow.length; ++i)
	{
		if (vRow[i].cells[indexOfCell].innerText == val)
			return i;
	}
	
	return -1;
}
function indexOfRow(ctrl, val)
{
	var tab = ctrl.selectTab;
	var vRow = tab.rows;
	
	for (i = 0; i < vRow.length; ++i)
	{
		if (vRow[i].value == val)
			return i;
	}
	
	return -1;
}
function getRowByValue(ctrl, val)
{
	var idx = indexOfRow(ctrl, val);
	if (-1 != idx)
		return ctrl.selectTab.rows[idx];
		
	return null;
}
function getAllRows(ctrl)
{
	return ctrl.selectTab.rows;
}
function clearParameters(f, paramName)
{
	eval("var v = f." + paramName);
	
	if (undefined == v)
	{
		return;
	}
	else if (undefined == v.length)
	{
		f.removeChild(v);
	}
	else
	{
		var i;
		for (i = v.length - 1; 0 <= i; --i)
		{
			f.removeChild(v[i]);
		}
	}
}
function appendParameters(f, paramName, ctrl, ctrlProp)
{
	var rows = getAllRows(ctrl);
	var i;
	for (i = 0; i < rows.length; ++i)
	{
		var val;
		eval("val = rows[i]." + ctrlProp + ";");
		appendParameter(f, paramName, val);
	}
}
function appendParameter(f, paramName, val)
{
	var p = document.createElement("<input>");
	p.type = "hidden";
	p.name = paramName;
	p.value = val;
	f.appendChild(p);
}
function setParameters(f, paramName, ctrl, ctrlProp)
{
	clearParameters(f, paramName);
	appendParameters(f, paramName, ctrl, ctrlProp);
}
function setParameter(f, paramName, val)
{
	clearParameters(f, paramName);
	appendParameter(f, paramName, val);
}
function initProgress(ctrl)
{
}
function jumpToPage(url)
{
	location = url;
}
